
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title> 密码修改 </title>
  <link rel="stylesheet" type="text/css" href="/css/style.css" />

  <style type="text/css">
    *, body {
      margin: 0px;
      font-family: '微软雅黑';
    }
  </style>
  <script src="/js/jquery-1.10.2.min.js"></script>
  <script src="/js/jquery.form.js"></script>
  <script src="/js/generic.js"></script>
</head>

<body>

<table class="maintable">
  <tr>
    <td class="rightmaintd" valign="top">
      <div class="searchdiv">
        <a href="/logout">&lt; 返回</a>
        <div class="rightsearchdiv"><!-- name --></div>
      </div>
      <table class="leftrighttable" border="0">
        <tr>
          <td class="lefttdtable" width="50%" valign="top">
            <table cellpadding="6">
              <tr>
                <td class="tdname">新密码</td>
                <td>
                  <input type="text" id="password" class="textinput" maxlength="50">
                </td>
              </tr>
              <tr>
                <td class="tdname">新密码确认</td>
                <td>
                  <input type="text" id="passwordagain" class="textinput" maxlength="50">
                </td>
              </tr>
            </table>
          </td>
          <td class="righttdtable" width="50%" valign="top"></td>
        </tr>

      </table>

      <div class="shenhediv">
        <input type="button" value="保 &nbsp; &nbsp; 存" class="baocunbtn"  id="Sbutton">
        <span id="lrxx"></span>
      </div>
      <span id="Id" th:text="${userid}" class="shownone">Id</span>
    </td>
  </tr>
</table>

</body>
</html>
<script type="text/javascript">


  //保存
  var saveButton = document.getElementById('Sbutton');
  saveButton.addEventListener('click', ()=> {
    var password1=document.getElementById('password').value;
    var password2=document.getElementById('passwordagain').value;
    var userid=document.getElementById('Id').textContent
    if (password1 == "") {
      alert("请输入新密码");
      return;
    }
    if (password2 =='') {
      alert("请确认新密码");
      return;
    }
    if (password2 !=password1) {
      alert("两次密码不一致");
      return;
    }
    var data = {
      userid: userid,
      password:password1
    };
    var xhr = new XMLHttpRequest();
    xhr.open('PUT', '/user/updatepassword/', true);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.onreadystatechange = function() {
      if (xhr.readyState === 4 && xhr.status === 200) {
        // 请求完成并成功处理后的回调函数
        console.log('数据已成功发送至后端');
        alert("保存成功！请重新登录");
        window.location.href="/logout";
      }
    };
    xhr.send(JSON.stringify(data));
  });

</script>